//
//  Joystick.swift
//  BombCar
//
//  Created by 时空矩人 on 16/5/1.
//  Copyright © 2016年 时空矩人. All rights reserved.
//

import SpriteKit

class Joystick: SKNode {
    
    //MARK: - 变量&常量
    //thumb回到控制组件中心的时间
    let thumbSpringBackDuration = 0.3
    //用来放整个控制组件
    var thumbNode: SKSpriteNode!
    //是否正在控制状态
    var isTracking: Bool = true
    //线速度
    var velocity: CGPoint!
    //thumb的最大移动距离
    var travelLimit: CGPoint!
    //角速度
    var angularVelocity: Float = 0.0
    //整个孔子去的尺寸
    var size: Float = 0.0
    
    //MARK: - 自定义方法
    //MARK: 私有方法
    private func anchorPointInPoints() -> CGPoint {
        return CGPointZero
    }
    
    private func resetVelocity() {
        isTracking = false
        velocity = CGPointZero
        
        let easeOut = SKAction.moveTo(self.anchorPointInPoints(), duration: thumbSpringBackDuration)
        easeOut.timingMode = SKActionTimingMode.EaseOut
        thumbNode.runAction(easeOut)
    }
    
    //MARK: 对象方法
    func setThumb(aNode: SKSpriteNode) {
        
        self.userInteractionEnabled = true
        velocity = CGPointZero
        thumbNode = aNode
        self.addChild(thumbNode)
        
    }
    
    func setThumbAndBackDrop(aNode aNode: SKSpriteNode, bgNode: SKSpriteNode) {
        
        
        bgNode.position = self.anchorPointInPoints()
        self.size = Float(bgNode.size.width)
        self.addChild(bgNode)
        
        self.setThumb(aNode)
    }
    
    //MARK: - 系统方法
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let touchPoint = touch?.locationInNode(self)
        
        if isTracking == false && CGRectContainsPoint(thumbNode.frame, touchPoint!) {
            isTracking = true
        }
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let touchPoint = touch!.locationInNode(self)
        
        //size取决于控制器背景图片的尺寸, 这里的控制器图片大小为135*135
        if isTracking == true && sqrt(powf(Float((touchPoint.x - self.anchorPointInPoints().x)), 2) + powf(Float((touchPoint.y - self.anchorPointInPoints().y)), 2)) < size {
            
            //thumbNode.size由thumbNode的图片决定, 这里图片大小60x60
            //如果手指点击的地方在thumbNode里面
            if sqrt(powf(Float((touchPoint.x - thumbNode.position.x)), 2) + powf(Float((touchPoint.y - thumbNode.position.y)), 2)) <= Float(thumbNode.size.width) {
                
                let moveDifference = CGPoint(x: touchPoint.x - self.anchorPointInPoints().x, y: touchPoint.y - self.anchorPointInPoints().y)
                
                thumbNode.position = CGPoint(x: self.anchorPointInPoints().x + moveDifference.x, y: self.anchorPointInPoints().y + moveDifference.y)
            } else {
                
                let dX = touchPoint.x - self.anchorPointInPoints().x
                let dY = touchPoint.y - self.anchorPointInPoints().y
                let magV = sqrt(dX * dX + dY * dY)
                let aX = self.anchorPointInPoints().x + dX / magV * thumbNode.size.width
                let aY = self.anchorPointInPoints().y + dY / magV * thumbNode.size.width
                thumbNode.position = CGPoint(x: aX, y: aY)
            }
            
        }
        
        velocity = CGPoint(x: (thumbNode.position.x - self.anchorPointInPoints().x), y: (thumbNode.position.y - self.anchorPointInPoints().y))
        angularVelocity = -atan2f(Float(thumbNode.position.x - self.anchorPointInPoints().x), Float(thumbNode.position.y - self.anchorPointInPoints().y))
        
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.resetVelocity()
    }
    
    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        self.resetVelocity()
    }

}
